package com.zHan.sky.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zHan.sky.common.contant.MessageConstant;
import com.zHan.sky.common.contant.StatusConstant;
import com.zHan.sky.common.exception.DeletionNotAllowedException;
import com.zHan.sky.common.result.Result;
import com.zHan.sky.entity.Category;
import com.zHan.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理
 */
@RestController("adminCategoryController")
@Slf4j
@RequestMapping("/admin/category")
@Api(tags = "分类相关接口")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类信息
     *
     * @param category
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增分类信息")
    public Result<String> save(@RequestBody Category category) {
        categoryService.save(category);
        return Result.success("添加成功");
    }

    /**
     * 分类的分页查询
     *
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分类的分页查询")
    public Result<Page> page(int page, int pageSize, String name, Integer type) {
        //创建分页构造器
        Page<Category> pageInfo = new Page<>(page, pageSize);
        //创建条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.eq(type != null, Category::getType, type)
                .like(StringUtils.isNotEmpty(name), Category::getName, name);
        //添加排序条件，根据sort进行升序排序
        queryWrapper.orderByAsc(Category::getSort);
        categoryService.page(pageInfo, queryWrapper);
        return Result.success(pageInfo);
    }


    /**
     * 根据id删除分类
     *
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation(value = "根据id删除分类")
    public Result<String> remove(Long id) {
        Category category = categoryService.getById(id);
        if (category.getStatus() == 1) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_IS_ENABLE);
        }
        categoryService.removeById(id);
        return Result.success("分类删除成功");
    }


    /**
     * 修改分类信息
     *
     * @param category
     * @return
     */
    @PutMapping
    @ApiOperation(value = "修改分类信息")
    public Result<String> update(@RequestBody Category category) {
        categoryService.updateById(category);
        return Result.success("分类信息修改成功");
    }


    /**
     * 根据类型查询分类数据
     *
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "根据条件查询分类数据")
    public Result<List<Category>> list(Category category) {
        //创建条件构造器对象
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        Integer categoryType = category.getType();
        queryWrapper.eq(categoryType != null, Category::getType, categoryType)
                .eq(Category::getStatus, StatusConstant.ENABLE);
        //添加排序条件
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        //执行方法
        List<Category> categories = categoryService.list(queryWrapper);

        return Result.success(categories);
    }

    /**
     * 启用禁用分类
     *
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation(value = "启用禁用分类")
    public Result<String> status(@PathVariable Integer status, @RequestParam Long id) {
        //条件构造器对象
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getId, id);
        Category Category = categoryService.getOne(queryWrapper);

        //执行方法
        Category.setStatus(status);
        categoryService.updateById(Category);

        return Result.success("停用员工成功");
    }

}
